package com.cleverest.cleverestdiary.service.impl;

import com.cleverest.cleverestdiary.mapper.ScheduleMapper;
import com.cleverest.cleverestdiary.pojo.Article;
import com.cleverest.cleverestdiary.pojo.PageBean;
import com.cleverest.cleverestdiary.pojo.Schedule;
import com.cleverest.cleverestdiary.service.ScheduleService;
import com.cleverest.cleverestdiary.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 日程服务实现类，实现了日程相关的业务操作方法
 */
@Service
public class ScheduleServiceImpl implements ScheduleService {

    @Autowired
    private ScheduleMapper scheduleMapper;

    @Override
    public void add(Schedule schedule) {
        // 补充属性值
        schedule.setCreateTime(LocalDateTime.now());
        schedule.setUpdateTime(LocalDateTime.now());

        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        schedule.setCreateUser(userId);

        scheduleMapper.add(schedule);
    }

    @Override
    public PageBean<Schedule> list(Integer pageNum, Integer pageSize, Integer categoryId, Integer state, Integer emergencyDegree, String keyword) {
        //1. 创建PageBean对象
        PageBean<Schedule> pb = new PageBean<>();

        //2. 开启分页查询
        PageHelper.startPage(pageNum, pageSize);

        //3. 调用Mapper查询
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        List<Schedule> as = scheduleMapper.list(userId, categoryId, state, emergencyDegree, keyword);
        //Page中提供了方法，可以提供PageHelper分页查询后得到的总记录条数和当前页数据。
        Page<Schedule> p = (Page<Schedule>) as;

        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public Schedule findById(Integer id) {
        Schedule schedule = scheduleMapper.findById(id);
        return schedule;
    }

    @Override
    public void update(Schedule schedule) {
        schedule.setUpdateTime(LocalDateTime.now());
        scheduleMapper.update(schedule);
    }

    @Override
    public void delete(Integer id) {
        scheduleMapper.delete(id);
    }

    @Override
    public PageBean<Schedule> searchByKeyword(Integer pageNum, Integer pageSize, String keyword) {
        // 1. 创建PageBean对象
        PageBean<Schedule> pb = new PageBean<>();

        // 2. 开启分页查询
        PageHelper.startPage(pageNum, pageSize);

        // 3. 调用Mapper查询
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        List<Schedule> schedules = scheduleMapper.searchByKeyword(userId, keyword);
        // Page中提供了方法，可以提供PageHelper分页查询后得到的总记录条数和当前页数据。
        Page<Schedule> p = (Page<Schedule>) schedules;

        // 把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public void updateState(Schedule schedule) {
        schedule.setUpdateTime(LocalDateTime.now());
        scheduleMapper.updateState(schedule);
    }

    @Override
    public PageBean<Schedule> searchByDate(LocalDate date, Integer pageNum, Integer pageSize) {
        PageBean<Schedule> pb = new PageBean<>();
        PageHelper.startPage(pageNum, pageSize);
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        List<Schedule> schedules = scheduleMapper.searchByDate(date, userId);
        Page<Schedule> p = (Page<Schedule>) schedules;
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }
}